<% include ../header.ejs %>
<script type="text/javascript" src="/angular/angular.min.js"></script>
<div ng-app="starter">
    <div class="container" ng-controller="GoldCtrl">
        <div class="row">
            <div class="col-md-4 col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        黄金钱包
                        <span ng-bind="(hjqbPrice/ldjPriceBuy*1000).toFixed(3)"></span>
                    </div>
                    <div class="panel-body">
                        <div>
                            当前金价: <span ng-bind="hjqbPrice" style="color: lightcoral; font-size: 24px"></span>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <div>
                            低于: <input type="text" id="hjqbMin">提示
                        </div>
                        <div>
                            高于: <input type="text" id="hjqbMax">提示
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-heading">金生宝 <span ng-bind="(jsbPriceBuy/ldjPriceBuy*1000).toFixed(3)"></span></div>
                    <div class="panel-body">
                        <div>
                            买入金价: <span ng-bind="jsbPriceBuy" style="color: lightcoral; font-size: 24px"></span>
                        </div>
                        <div>
                            卖出金价: <span ng-bind="jsbPriceSell" style="color: lightblue; font-size: 18px"></span>
                        </div>
                        <div>
                            更新时间: <span ng-bind="jsbPriceUpdated" style="color: lightblue; font-size: 18px"></span>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <div>
                            低于: <input type="text" id="jsbMin">提示
                        </div>
                        <div>
                            高于: <input type="text" id="jsbMax">提示
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4 col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-heading">伦敦金</div>
                    <div class="panel-body">
                        <div>
                            金价: <span ng-bind="ldjPriceBuy" style="color: lightcoral; font-size: 24px"></span>
                        </div>
                        <div>
                            更新时间: <span ng-bind="ldjPriceUpdated" style="color: lightblue; font-size: 18px"></span>
                        </div>
                    </div>
                    <div class="panel-footer">
                    </div>
                </div>
            </div>
            <div class="col-md-4">
            </div>
        </div>
        <div class="row">
            <div class="panel panel-default">
                <div class="panel-heading">配置</div>
                <div class="panel-body">
                    <div>
                        价格刷新频率: <input type="number" value="5000" ng-model="freshTime">ms
                        <button class="btn btn-primary btn-xs" ng-click="freshTime=1000">快(1000ms)</button>
                        <button class="btn btn-default btn-xs" ng-click="freshTime=5000">正常(5000ms)</button>
                        <button class="btn btn-warning btn-xs" ng-click="freshTime=11000">慢(11000ms)</button>
                    </div>
                    <div>
                        消息通知显示: <input type="number" value="3000" id="notificationShowTime">ms
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script>
    if (window.Notification){
        Notification.requestPermission();
    }
    angular.module('starter', []).controller('GoldCtrl', ['$scope', '$timeout', function($scope, $timeout) {
        $scope.freshTime = 0;
        function runGetPriceInterval(){
            var freshTime = $scope.freshTime;
            $timeout(function(){
                $scope.freshTime = $scope.freshTime || 5000;
                getHJQBNowGold();
                getJSBNowGold();
                getLDJGoldPrice();
                runGetPriceInterval();
            }, freshTime);
        }
        runGetPriceInterval();

        // 获取黄金钱包价格
        function getHJQBNowGold(){
            $.ajax({
                type: "GET",
                url: "/goldPrice",
                success: function(data){
                    var price = data.price/100;
                    $scope.hjqbPrice = price;
                    checkoutNatifacation('HJQB', price);
                },
                error: function(){
                    $('#hjqbPrice').html('--');
                }
            });
        }
        // 获取金生宝价格
        function getJSBNowGold() {
            var url = 'https://login.vip9999.com/?s=api-getgold&callback=?';
            $.getJSON(url, function(response) {
                $scope.jsbPriceBuy = response.results.buy;
                $scope.jsbPriceSell = response.results.sell;
                $scope.jsbPriceUpdated = response.results.converted_updated;
                checkoutNatifacation('JSB', response.results.buy-0);
            }, "json")
        }
        //获得伦敦金价格
        function getLDJGoldPrice(){
            $.ajax({
                type: "GET",
                url: "/ldjPrice",
                success: function(data){
                    $scope.ldjPriceBuy = data.data.split(' ')[0].split('#')[1];
                    $scope.ldjPriceUpdated = data.data.split(' ')[2];
                },
                error: function(){

                }
            });
        }
        // 消息通知
        function checkoutNatifacation(type, price){
            var hjqbMin = parseFloat($('#hjqbMin').val());
            var hjqbMax = parseFloat($('#hjqbMax').val());
            var jsbMIn = parseFloat($('#jsbMin').val());
            var jsbMax = parseFloat($('#jsbMax').val());

            if(type == 'HJQB' && hjqbMin && hjqbMin > price){
                notificationShow('/images/goDown.png', '/images/sanxing.mp3', '黄金钱包', '价钱跌到' + price);
            }
            if (type == 'HJQB' && hjqbMax && hjqbMax < price){
                notificationShow('/images/goUp.png', '/images/dada.mp3', '黄金钱包', '价钱涨到' + price);
            }
            if(type == 'JSB' && jsbMIn && jsbMIn > price){
                notificationShow('/images/goDown.png', '/images/sanxing.mp3', '金生宝', '价钱跌到' + price);
            }
            if (type == 'JSB' && jsbMax && jsbMax < price){
                notificationShow('/images/goUp.png', '/images/dada.mp3', '金生宝', '价钱涨到' + price);
            }
        }
        function notificationShow(icon, tipAudioUrl, title, message){
            if (!window.Notification){
                tipSound(tipAudioUrl);
                return ;
            }

            var notificationShowTime = parseInt($('#notificationShowTime').val()) || 3000;
            var notification = new Notification(title,{
                body: message,
                icon: icon
            });
            notification.onshow = function(){
                tipSound(tipAudioUrl);
                setTimeout(function(){
                    notification.close();
                }, notificationShowTime);
            };
        }
        function tipSound(url){
            url = url || '/images/dada.mp3';
            var audio = document.createElement('audio');
            audio.src = url;
            audio.play();
        }
    }]);
</script>


<!--金大师价格-->
<!--<script>-->
    <!--jQuery.support.cors = true;-->
    <!--setInterval(timedMsg,3000);-->
    <!--timedMsg();-->
    <!--function timedMsg() {-->
        <!--$.ajax({-->
            <!--type: "GET",-->
            <!--url: "http://jry.baidao.com/api/hq/npdata.do?markettype=shj&ids=101,102",-->
            <!--dataType: 'jsonp',-->
            <!--data: '',-->
            <!--jsonp: 'JSONP',-->
            <!--async: false,-->
            <!--success: function (data) {-->
                <!--console.log(data);-->
            <!--}-->
        <!--});-->
    <!--}-->
<!--</script>-->